package com.codeyourlife.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.codeyourlife.dao.UserRelatedCourseDao;
import com.codeyourlife.entity.Course;
import com.codeyourlife.entity.UserFavCourse;
import com.codeyourlife.entity.UserHistory;
import com.codeyourlife.entity.UserLearning;

@Repository("UserRelatedCourseDao")
public class UserRelatedCourseDaoImpl implements UserRelatedCourseDao {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void addUpCourse(Course course) {
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			session.save(course);
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:addUpCourse " + e.toString());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Course> getUserUpCourse(int user_id) {
		System.out.println("user_id " + user_id);
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			Query queryCourseId = session.createQuery("from Course uc where uc.owner_id = '" + user_id + "'");
			List<Course> listCourse = queryCourseId.list();
			// session.close();
			return listCourse;
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:getUserUpCourse " + e.toString());
		}
		return null;
	}

	public void addUserFavCourse(UserFavCourse userFav) {
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			session.save(userFav);
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:addUserFavCourse " + e.toString());
		}
	}

	public void deleUserFavCourse(UserFavCourse userFav) {
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			session.createQuery("delete UserFavCourse uf where uf.user_id = '"
					+ userFav.getUser_id() + "'and uf.favcourse_id='" + userFav.getFavcourse_id() + "' ");

		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:deleUserFavCourse " + e.toString());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Course> getfavoriteCourse(int user_id) {
		List<Course> favoriteCourseList = new ArrayList<Course>();
		List<UserFavCourse> getfavoriteCourseId = new ArrayList<UserFavCourse>();

		try {
			Session session = this.getSessionFactory().getCurrentSession();
			Query queryfavCourse = session.createQuery("from UserFavCourse c where c.user_id = '" + user_id + "' ");
			// session.close();
			getfavoriteCourseId = queryfavCourse.list();
			for (int i = 0; i < getfavoriteCourseId.size(); i++) {
				int courseid = getfavoriteCourseId.get(i).getFavcourse_id();
				Query queryCourseById = session.createQuery("from Course c where c.id = '" + courseid + "'");
				favoriteCourseList.add((Course) queryCourseById.list().get(0));
			}
			return favoriteCourseList;
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:getfavoriteCourse " + e.toString());
		}
		return null;

	}

	public void addUserLearning(UserLearning userLearning) {
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			session.save(userLearning);
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:addUserLearning " + e.toString());
		}
	}

	@SuppressWarnings("unchecked")
	public void updateUserLearning(UserLearning userLearning) {
		Query query = this.getSessionFactory().getCurrentSession()
				.createQuery("from UserLearning u where u.id='" + userLearning.getId() + "'");
		List<UserLearning> list = query.list();
		UserLearning ul_new = list.get(0);

		System.out.println("UserRelatedDaoImpl:updateUserLearning " + ul_new.toString());
		ul_new.setLast_time(userLearning.getLast_time());
		ul_new.setLesson_id(userLearning.getLesson_id());

		System.out.println("UserRelatedDaoImpl:updateUserLearning " + ul_new.toString());
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(ul_new);
		session.getTransaction().commit();
		session.close();
	}
	
	public void deleteUserLearning(UserLearning userLearning) {
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			session.createQuery("delete UserLearning ul where ul.user_id = '"
					+ userLearning.getUser_id() + "'and ul.favcourse_id='" + userLearning.getCourse_id() + "' ");
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:deleUserFavCourse " + e.toString());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Course> getUserLearning(int user_id) {
		List<Course> LearningCourseList = new ArrayList<Course>();
		List<UserLearning> getLearningCourseId = new ArrayList<UserLearning>();
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			Query queryUserLearning = session.createQuery("from UserLearning u where u.user_id = '" + user_id + "'");
			getLearningCourseId = queryUserLearning.list();
			for (int i = 0; i < getLearningCourseId.size(); i++) {
				int courseid = getLearningCourseId.get(i).getCourse_id();
				Query queryCourseById = session.createQuery("from Course c where c.id = '" + courseid + "'");
				LearningCourseList.add((Course) queryCourseById.list().get(0));
			}
			return LearningCourseList;
			// session.close();
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:getUserinfo " + e.toString());
		}
		return null;
	}

	public void addUserHistory(int user_id, UserLearning userLearning) {
		UserHistory uHistory = new UserHistory();
		uHistory.setAdd_time(userLearning.getAdd_time());
		uHistory.setCourse_id(userLearning.getCourse_id());
		uHistory.setFinish_time(userLearning.getLast_time());
		uHistory.setUser_id(userLearning.getUser_id());

		try {
			Session session = this.getSessionFactory().getCurrentSession();
			session.save(uHistory);
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:addUserLearning " + e.toString());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Course> getUserHistory(int user_id) {
		List<Course> HistoryCourseList = new ArrayList<Course>();
		List<UserHistory> getHistoryCourseId = new ArrayList<UserHistory>();
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			Query queryUserhistory = session.createQuery("from UserHistory u where u.user_id = '" + user_id + "'");
			getHistoryCourseId = queryUserhistory.list();

			for (int i = 0; i < getHistoryCourseId.size(); i++) {
				int courseid = getHistoryCourseId.get(i).getCourse_id();
				Query queryCourseById = session.createQuery("from Course c where c.id = '" + courseid + "'");
				HistoryCourseList.add((Course) queryCourseById.list().get(0));
			}
			// session.close();
			return HistoryCourseList;
		} catch (Exception e) {
			System.out.println("UserReleatedDaoImpl:getUserinfo " + e.toString());
		}
		return null;
	}
}
